/**
 * Copyright 2013 Mr.Hai Mobile Developer. All rights reserved.
 * HAI PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
 */
package com.hector.meinewerk.controller;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;

import com.hector.meinewerk.client.view.common.ScreenIntroduce1;
import com.hector.meinewerk.client.view.common.ScreenIntroduce2;
import com.hector.meinewerk.client.view.common.ScreenIntroduce3;
import com.hector.meinewerk.common.ActionEvent;
import com.hector.meinewerk.common.ActionEventConstant;
import com.hector.meinewerk.common.BaseActivity;
import com.hector.meinewerk.lib.network.http.HTTPRequest;
import com.hector.meinewerk.model.UserModel;

/**
 * 
 * User controller
 * 
 * @author: HaiTC3
 * @version: 1.1
 * @since: 1.0 | Mar 6, 2013
 */
public class UserController extends AbstractController {

	static UserController instance;

	protected UserController() {
	}

	public static UserController getInstance() {
		if (instance == null) {
			instance = new UserController();
		}
		return instance;
	}

	/**
	 * Xu ly chuyen activity
	 */
	@Override
	public void handleSwitchActivity(ActionEvent e) {
		Activity base = (Activity) e.sender;
		Intent intent;
		Bundle extras;
		switch (e.action) {
		default:
			break;
		}
	}

	/**
	 * xu ly su kien tu view
	 */
	public void handleViewEvent(ActionEvent e) {
		HTTPRequest request = null;
		boolean isProcessRequest = true;
		BaseActivity baseActivity = (BaseActivity) e.sender;
		if (baseActivity != null && e.isBlockRequest) {
			if (baseActivity.checkExistRequestProcessing(e.action)) {
				isProcessRequest = false;
			}
		}
		if (isProcessRequest) {
			switch (e.action) {
			case ActionEventConstant.REQUEST_CATERGORIES:
				UserModel.getInstance().requestCatergoriesHTTP(e);
				break;
			case ActionEventConstant.REQUEST_CATERGORY:
				UserModel.getInstance().requestCatergoryHTTP(e);
				break;
			case ActionEventConstant.REQUEST_PRODUCT:
				UserModel.getInstance().requestProductHTTP(e);
				break;
			case ActionEventConstant.REQUEST_CATERGORIES_TREE:
				UserModel.getInstance().requestCatergoriesTreeHTTP(e);
				break;
			case ActionEventConstant.SHOW_SCREEN_1: {
				Activity base = (Activity) e.sender;
				Intent intent;
				Bundle extras;
				intent = new Intent(base, ScreenIntroduce1.class);
				extras = (Bundle) e.viewData;
				intent.putExtras(extras);
				base.startActivity(intent);
				break;
			}
			case ActionEventConstant.SHOW_SCREEN_2: {
				Activity base = (Activity) e.sender;
				Intent intent;
				Bundle extras;
				intent = new Intent(base, ScreenIntroduce2.class);
				extras = (Bundle) e.viewData;
				intent.putExtras(extras);
				base.startActivity(intent);
				break;
			}
			case ActionEventConstant.SHOW_SCREEN_3: {
				Activity base = (Activity) e.sender;
				Intent intent;
				Bundle extras;
				intent = new Intent(base, ScreenIntroduce3.class);
				extras = (Bundle) e.viewData;
				intent.putExtras(extras);
				base.startActivity(intent);
				break;
			}
			default:
				break;
			}
			if (request != null && baseActivity != null) {
				baseActivity.addProcessingRequest(request, e.isBlockRequest);
				e.request = request;
			}
		}
	}
}
